package core;

import pieces.Piece;
import player.ComputerPlayer;
import player.HumanPlayer;
import player.PederPlayer;
import player.Player;
import core.Color;

/**
 * Class: Game
 * Responsibilities: Initialize and keep track of players and game state.
 * Collaborators: Board, Player.
 * @author Tobias
 *
 */
public class Game {
	private Board board;
	private boolean whiteKingThreatened;
	private boolean blackKingThreatened;
	private Player player1; // White player
	private Player player2; // Black player
	private boolean whitesTurn;
	private boolean gameOver;

	/**
	 * Default constructor, creates what you need to play a game.
	 */
	public Game() {
		board = new Board();
		whiteKingThreatened = false;
		blackKingThreatened = false;

		player1 = new HumanPlayer(Color.WHITE);
		player2 = new HumanPlayer(Color.BLACK);

		whitesTurn = true;
		gameOver = false;
	}

	/**
	 * Method that starts the show. Create a new game-object, and start
	 * making moves.
	 * @param args - Not used.
	 */
	public static void main(String[] args) {
		Game g = new Game();
		while(!g.isGameOver()) {
			g.delegateMove(g.board);
			g.board.getGuiBoard().drawBoard(g.board);
			Piece piece = g.getBoard().finished();
			if(piece != null) {
				if(piece.isWhite())
					System.out.print("White ");
				else
					System.out.print("Black ");
				System.out.println(" is the winner!!!1");
				break;
			}
		}
	}

	/**
	 * Delegates a move to a player, depending on who's turn it is.
	 * @param An instance of board.
	 */
	private void delegateMove(Board b) {
		if(whitesTurn) getMoveAndCheck(player1, b, Color.WHITE);
		else getMoveAndCheck(player2, b, Color.BLACK);
		whitesTurn = !whitesTurn;
	}

	/**
	 * Get a move from a player, and check if it was a legal move.
	 * @param The player doing the move
	 * @param An instance of board.
	 */
	public void getMoveAndCheck(Player p, Board b, Color color) {
		while(true) {
			Move m = p.makeMove(b,b.getPiecesByColor(Color.WHITE),b.getPiecesByColor(Color.BLACK));
			if(b.isLegalMove(m,color)) return;
		}
	}

	//TODO Add at least SOME javadoc to the getters and setters.
	//And maybe remove some of them..

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public boolean isWhiteKingThreatened() {
		return whiteKingThreatened;
	}

	public void setWhiteKingThreatened(boolean whiteKingThreatened) {
		this.whiteKingThreatened = whiteKingThreatened;
	}

	public boolean isBlackKingThreatened() {
		return blackKingThreatened;
	}

	public void setBlackKingThreatened(boolean blackKingThreatened) {
		this.blackKingThreatened = blackKingThreatened;
	}

	public Player getPlayer1() {
		return player1;
	}

	public void setPlayer1(Player player1) {
		this.player1 = player1;
	}

	public Player getPlayer2() {
		return player2;
	}

	public void setPlayer2(Player player2) {
		this.player2 = player2;
	}

	public boolean isWhitesTurn() {
		return whitesTurn;
	}

	public void setWhitesTurn(boolean whitesTurn) {
		this.whitesTurn = whitesTurn;
	}

	public boolean isGameOver() {
		return gameOver;
	}

	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}
}
